﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Extensions;
using VOL.Domain.Infrastructure;
using VOL.Domain.IServices.WarehouseManagement;
using VOL.Entity.DomainModels;

namespace VOL.Domain.Services.WarehouseManagement
{
    /// <summary>
    /// 盘点任务
    /// </summary>
    public class InventoryManagementServices : IInventoryManagementServices
    {
        private readonly IBaseRepository<InventoryManagement> _inventoryManagementRepository;

        public InventoryManagementServices(IBaseRepository<InventoryManagement> inventoryManagementRepository)
        {
            _inventoryManagementRepository = inventoryManagementRepository;
        }
        /// <summary>
        /// 新增盘点单
        /// </summary>
        /// <param name="inventoryManagement"></param>
        /// <returns></returns>
        public async Task<int> AddInventoryManagement(InventoryManagement inventoryManagement)
        {
            return await _inventoryManagementRepository.Add(inventoryManagement);
        }
        /// <summary>
        /// 盘点单的显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<InventoryManagement>> GetAllInventoryManagement(string? taskCode,int? status, int pageIndex=1, int pageSize=5)
        {
            var inventoryManagementlist = _inventoryManagementRepository.GetAll();
            inventoryManagementlist = inventoryManagementlist.WhereIF(taskCode!=null,m=>m.TaskCode.Contains(taskCode));
            inventoryManagementlist = inventoryManagementlist.WhereIF(status != null, m => m.Status==status);
            var total= inventoryManagementlist.Count();
            var items= inventoryManagementlist.OrderBy(m=>m.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return inventoryManagementlist.ToList();
        }

        /// <summary>
        /// 盘点单的修改
        /// </summary>
        /// <param name="inventoryManagement"></param>
        /// <returns></returns>
        public async Task<int> UpdateInventoryManagement(InventoryManagement inventoryManagement)
        {
            return await _inventoryManagementRepository.Update(inventoryManagement);
        }

        /// <summary>
        /// 删除盘点单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteInventoryManagement(int id)
        {
            var inventoryManagement = (await _inventoryManagementRepository.FindAsync(m=> m.Id == id)).FirstOrDefault();
            if (inventoryManagement == null) return 0;
            return await _inventoryManagementRepository.Delete(inventoryManagement);
        }

       
    }
}
